<?php

namespace app\admin\controller\pay;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 支出明细
 *
 * @icon fa fa-circle-o
 */
class Apply extends Backend
{

    /**
     * Pay模型对象
     */
    protected $model = null;
    protected $projectModel = null;
    protected $paymodel=null;
    protected $searchfields;
    protected $apply_logModel=null;
    protected $companyModel=null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('PayApply');
        $this->projectModel = model('PayProject');
        $this->paymodel=model('pay');
        $this->companyModel=model('company');
        $this->apply_logModel=model('PayApplyLog');
        $this->view->assign('project', $this->projectModel->field('id,name')->select());
        
            $pid = input('pid','0', 'intval');    
            $status=array("0"=>__('Unpaid'),"1"=>__('Waiting'),"2"=>__('Paid'));
            $this->view->assign("status", $status);
        $this->view->assign("pid", $pid);
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            return $this->handleSearch($this->searchfields);
        }
        return $this->view->fetch();
    }

    public function paylogList()
    {
        if ($this->request->isAjax()) {
            $apid = input('apid','0', 'intval');   
           $list= $this->apply_logModel->with('pay')->where('applyid','=',$apid)->select();
           $result = array("rows" => $list);
            return   $result;
        }
    }

    public function addapply()
    {
        $pid=input('get.pid',0);
        $project=$this->projectModel->where('id','=',$pid)->find();
        if(!empty($project)){
        $this->view->assign('paylist',  $this->paymodel->where('project_id','=',$pid)->select());
     // $this->view->assign('paylist',  $this->apply_logModel->where('project_id','=',$pid)->select());
        $this->view->assign('pmodel', $project);
        $this->view->assign('supplier',$this->companyModel->where('id','=',$project['suppliercid'])->find());
        
        }    
        return parent::create('form');
    }

    public function addLog()
    {
        if ($this->request->isPost()) {
            try
            {
            // $params = $this->request->post('row/a');
            $data=input('post.');
            $log= $data['log'];
            unset($data['log']);
            $data['state']=1;
            $data['addtime']=time();
            $data['updatetime']=time();
            $this->model->insert($data);
            $apid=$this->model->getLastInsID();
           // $log= $data['log'];
            foreach($log as $k => &$v){
                $v['applyid']= $apid;
           
            }
            $this->apply_logModel->insertAll($log);
            $this->success('数据添加成功','',array('applyid'=>$apid));
        }
        catch (\think\exception\PDOException $e)
        {
            $this->error($e->getMessage());
        }
         }
    }

    public function updateState()
    {
        $id=input('id','0', 'intval');  
        $this->model->where('id','=',$id)->update(['state'=>1]);
    }

    public function edit($ids = null)
    {
        return parent::modify($ids, 'form');
    }

    public function del($ids=null){
       // $id=input('id','0', 'intval');   
        $where = array(
            'id' => array('in',$ids),
            'state'=>0
        );
        $this->apply_logModel->where('applyid','in',$ids)->delete();
        parent::delete($where);
        
        
    }

    function view()
    { 
        $id=input('id','0', 'intval');  
        $apply=$this->model->with('project')->where('id','=',$id)->find();
        if(empty($apply))
        {
            return;
        }
        $projectid=$apply['projectid'];
        $project= $this->projectModel->with('supplier')->with('paycompany')->where('id','=',$projectid)->find();
       $paylist=$this->apply_logModel->with('pay')->where('applyid','=',$apply['id'])->select();

        $this->view->assign('apply', $apply);     
        $this->view->assign('project', $project);    
        $this->view->assign('paylist', $paylist); 
        $totalAmount=0;
        $this->view->assign('totalAmount');
        $paidAmount=0;
        $this->view->assign('paidAmount');
        $payAmount=0;
        $this->view->assign('payAmount');
    // var_dump(json_encode($paylist));

        return $this->fetch('view');
    }

    protected function handleSearch($searchfields)
    { 
        $append = array(
           
        );
        list($where, $sort, $order, $offset, $limit,$orderParams) = $this->buildparams($searchfields, null, $append);
        $total = $this->model->where($where)->count();
        $list = $this->model->with('project')->where($where)->order($orderParams)->limit($offset, $limit)->select();
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }

    private function buildCommonSearch()
    {
        $where = array();
        $searchs = $this->request->request('query/a');
        if ($searchs['pid']) {
            $where[] = array('projectid', '=', $searchs['pid']);
        }
        return $where;
    }
}
